<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form :inline="true">
            <el-form-item :label="$t('shiJianXuanZe')+'：'" class="width100">
              <el-radio-group
                v-model="tableFrom.dateLimit"
                type="button"
                class="mr20"
                size="small"
                @change="selectChange(tableFrom.dateLimit)"
              >
                <el-radio-button v-for="(item, i) in fromList.fromTxt" :key="i" :label="item.val">{{
                  $t(item.text)
                }}</el-radio-button>
              </el-radio-group>
              <el-date-picker
                v-model="timeVal"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                size="small"
                type="daterange"
                placement="bottom-end"
                :placeholder="$t('ziDingYiShiJian')"
                style="width: 220px"
                @change="onchangeTime"
              />
            </el-form-item>
            <div>
              <el-form-item :label="$t('huiFuZhuangTai')+'：'" class="mr20">
                <el-select
                  v-model="tableFrom.isReply"
                  :placeholder="$t('qingXuanZeHuiFuZhuangTai')"
                  size="small"
                  class="selWidth"
                  clearable
                  @change="seachList"
                >
                  <el-option :label="$t('yiHuiFu')" :value="1" />
                  <el-option :label="$t('weiHuiFu')" :value="0" />
                </el-select>
              </el-form-item>
              <el-form-item :label="$t('pingJiaXingJi')+'：'" class="star">
                <div class="acea-row">
                  <el-rate v-model="tableFrom.star" @change="seachList" />
                  <el-button type="primary" size="mini" @click="clearRate">{{ $t('qingKongXingJi') }}</el-button>
                </div>
              </el-form-item>
            </div>

            <el-form-item :label="$t('shangPinSouSuo0')+'：'" class="mr20">
              <el-input
                v-model="tableFrom.productSearch"
                :placeholder="$t('qingShuRuShangPinMingCheng')"
                class="selWidth"
                size="small"
                clearable
              >
                <el-button slot="append" icon="el-icon-search" size="small" @click="seachList" />
              </el-input>
            </el-form-item>
            <el-form-item :label="$t('yongHuMingCheng')+'：'">
              <el-input
                v-model="tableFrom.nickname"
                :placeholder="$t('qingShuRuYongHuMingCheng')"
                class="selWidth"
                size="small"
                clearable
              >
                <el-button slot="append" icon="el-icon-search" size="small" @click="seachList" />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <el-button
          v-hasPermi="['merchant:product:reply:virtual']"
          size="small"
          type="primary"
          @click="add"
        >{{ $t('tianJiaXuNiPingLun') }}</el-button>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="mini" class="table">
        <el-table-column prop="id" label="ID" width="50" />
        <el-table-column :label="$t('shangPinXinXi')" min-width="200" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-if="scope.row.productName" class="demo-image__preview acea-row row-middle">
              <el-image
                style="width: 30px; height: 30px"
                :src="scope.row.productImage"
                :preview-src-list="[scope.row.productImage]"
                class="mr10"
              />
              <div class="info">{{ scope.row.productName }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="nickname" :label="$t('yongHuMingCheng')" min-width="100" />
        <el-table-column prop="star" :label="$t('pingJiaXingJi')" min-width="90" />
        <el-table-column :label="$t('pingJiaNeiRong')" min-width="250" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div class="mb5 content_font">{{ scope.row.comment }}</div>
            <template v-if="scope.row.pics.length && scope.row.pics[0]">
              <div class="demo-image__preview">
                <el-image
                  v-for="(item, index) in scope.row.pics"
                  :key="index"
                  :src="item"
                  class="mr5"
                  :preview-src-list="[item]"
                />
              </div>
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="merchantReplyContent" :label="$t('huiFuNeiRong')" min-width="200" :show-overflow-tooltip="true" />
        <el-table-column :label="$t('pingJiaShiJian')" min-width="150">
          <template slot-scope="scope">
            <span> {{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('caoZuo')" min-width="120" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['merchant:product:reply:comment']"
              type="text"
              size="small"
              class="mr10"
              @click="reply(scope.row.id)"
            >{{ $t('huiFu') }}</el-button>
            <el-button
              v-hasPermi="['merchant:product:reply:delete']"
              type="text"
              size="small"
              @click="handleDelete(scope.row.id, scope.$index)"
            >{{ $t('shanchu') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
      <el-dialog :title="$t('tiShi')" :visible.sync="dialogVisible" width="700px" z-index="4" :before-close="handleClose">
        <creat-comment :key="timer" @getList="seachList" />
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import creatComment from './creatComment.vue'
import { replyListApi, replyDeleteApi, replyCommentApi } from '@/api/store'
import { formatDates } from '@/utils/index'
export default {
  name: 'StoreComment',
  filters: {
    formatDate(time) {
      if (time !== 0) {
        const date = new Date(time * 1000)
        return formatDates(date, 'yyyy-MM-dd hh:mm')
      }
    }
  },
  directives: {
    // 计算是否滚动到最下面
    selectLoadMore: {
      bind(el, binding) {
        // 获取element-ui定义好的scroll盒子
        const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
        SELECTWRAP_DOM.addEventListener('scroll', function() {
          if (this.scrollHeight - this.scrollTop < this.clientHeight + 1) {
            binding.value()
          }
        })
      }
    }
  },
  components: { creatComment },
  data() {
    return {
      fromList: this.$constants.fromList,
      tableData: {
        data: [],
        total: 0
      },
      listLoading: true,
      tableFrom: {
        page: 1,
        limit: 20,
        isReply: '',
        dateLimit: '',
        star: null,
        nickname: '',
        productSearch: ''
      },
      timeVal: [],
      loading: false,
      dialogVisible: false,
      timer: ''
    }
  },
  watch: {
    $route(to, from) {
      this.getList()
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    clearRate() {
      this.tableFrom.star = null
      this.seachList()
    },
    seachList() {
      this.dialogVisible = false
      this.tableFrom.page = 1
      this.getList()
    },
    // 回复
    reply(id) {
      this.$prompt(this.$t('huiFuNeiRong'), {
        inputErrorMessage: this.$t('qingShuRuHuiFuNeiRong'),
        inputType: 'textarea',
        inputPlaceholder: this.$t('qingShuRuHuiFuNeiRong'),
        inputValidator: (value) => {
          if (!value) {
            return this.$t('shuRuBuNengWeiKong')
          }
        }
      })
        .then(({ value }) => {
          replyCommentApi({
            id: id,
            merchantReplyContent: value
          }).then((res) => {
            this.$message({
              type: 'success',
              message: this.$t('huiFuChengGong')
            })
            this.getList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: this.$t('quXiaoShuRu')
          })
        })
    },
    // 选择时间
    selectChange(tab) {
      this.timeVal = []
      this.tableFrom.page = 1
      this.getList()
    },
    add() {
      this.dialogVisible = true
      this.timer = new Date().getTime()
    },
    handleClose() {
      this.dialogVisible = false
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e
      this.tableFrom.dateLimit = e ? this.timeVal.join(',') : ''
      this.tableFrom.page = 1
      this.getList()
    },
    // 删除
    handleDelete(id, idx) {
      this.$modalSure().then(() => {
        replyDeleteApi(id).then(() => {
          this.$message.success(this.$t('shanChuChengGong'))
          this.tableData.data.splice(idx, 1)
        })
      })
    },
    // 列表
    getList() {
      this.listLoading = true
      if (this.tableFrom.star === 0) this.tableFrom.star = null
      replyListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.list
          this.tableData.total = res.total
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList()
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">
.table {
  ::v-deep .el-tooltip {
    white-space: inherit !important;
  }
}

.selWidth {
  width: 350px !important;
}
.star {
  ::v-deep.el-rate {
    line-height: 2.5 !important;
  }
}
.table {
  ::v-deep el-image__inner,
  .el-image__placeholder,
  .el-image__error {
    width: auto !important;
  }
}
.info {
  width: 63%;
}
</style>
